/**
 *
 */
package com.dhcc.android.base.common.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dhcc.android.base.R;
import com.dhcc.android.base.gis.SelectItemChangedListener;
import com.dhcc.android.base.gis.TouchLongToolForPointOnly;
import com.dhcc.android.base.utils.CommenUtil;

import java.text.DecimalFormat;

import srs.Geometry.IEnvelope;
import srs.tools.MapControl;

/**
 * 点击图上选点按钮弹出框
 *
 * @author pengling
 * @date 2015-6-30
 */
public class GetLocationByMapPoup {
    private MyPopupWindow popupwindow;
    private Activity activity;

    public GetLocationByMapPoup(Activity activity) {
        this.activity = activity;
    }

    @SuppressLint("ResourceAsColor")
    public void initPopWindow(View parent, final TextView edit_content, String title) {
        View view = null;
        if (popupwindow == null) {
            view = LayoutInflater.from(activity).inflate(
                    R.layout.util_layout_carrier, null);
            popupwindow = new MyPopupWindow(view, LayoutParams.FILL_PARENT,
                    LayoutParams.FILL_PARENT, true);
            popupwindow.setAnimationStyle(R.style.PopupCenterAnimation);
        }
        LinearLayout map_layout = (LinearLayout) view.findViewById(R.id.map_layout);
        View childView = (View) activity.getLayoutInflater().inflate(
                R.layout.util_dk_new, null);
//		((DemandMainActivity)activity).initDK(childView, map_layout,title);

        TextView title_textview = (TextView) childView
                .findViewById(R.id.title_textview);
        title_textview.setText(title);

        IEnvelope env = CommenUtil.getMap().getExtent();
        final MapControl mapControl = (MapControl) childView
                .findViewById(R.id.mapmain);

//		MapControl mcontrol = CommenUtil.getMapControl();//此界面的mapcontrol
        /*CommenLayer的点击事件*/
        TouchLongToolForPointOnly touchLongTool = new TouchLongToolForPointOnly();
        mapControl.ClearDrawTool();
        touchLongTool.setBuddyControl(mapControl);// mapControl为操作的地图控件
        touchLongTool.onClick(mapControl);
        touchLongTool.setEnable(true);
        // 设置选中或未选中条目时，触发的事件
        touchLongTool.zoom2Selected = new SelectItemChangedListener() {

            @Override
            public void doEventSettingsChanged(Object tar) {
                double[] lonlat = (double[]) tar;
                System.out.println("####经度：" + lonlat[0] + ";纬度" + lonlat[1]);
                DecimalFormat df = new DecimalFormat("######0.000000");
                edit_content.setText(df.format(lonlat[0]) + "," + df.format(lonlat[1]));//几何计算出结果显示
                dismiss();
				/*选中后要触发的事件*/
            }
        };
        mapControl.setDrawTool(touchLongTool);


        mapControl.getActiveView().FocusMap().setExtent(env);

        mapControl.getActiveView().FocusMap(CommenUtil.getMap());
        mapControl.getActiveView().FocusMap().setExtent(env);


        map_layout.setVisibility(View.VISIBLE);
        map_layout.addView(childView);
        mapControl.Refresh();

        //给map_layout增加子图层，显示地图
//		map_layout.addView(CommenUtil.getMapControl());
        //选好点后，将获取到的GPS直接 edit_content.setText(...);

        view.setBackgroundColor(R.color.black);
        popupwindow.update();
        popupwindow.showAtLocation(parent, Gravity.CENTER | Gravity.CENTER, 0,
                0);// 显示在最中央的位置

    }

    public void dismiss() {
        if (popupwindow != null) {
            popupwindow.dismiss();
        }
    }

}
